<template>
	<view v-if="goods_info.goods_name" class="goods-detail-container">
		<u-navbar></u-navbar>	
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :circular="true">
			<swiper-item v-for="(item,index) in goods_info.pics" :key="index">
				<image :src="item.pics_big" @click="preview(index)"></image>
			</swiper-item>
		</swiper>

		<view class="goods-info-box">
			<view class="price">¥ {{goods_info.goods_price}}</view>
			<view class="goods-info-body">
				<view class="goods-name">{{goods_info.goods_name}}</view>
				<view class="collect">
					<uni-icons type="star" size="18" color="gray"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<view class="yf">快递: 免运费</view>
		</view>

		<!-- 富文本解析器 -->
		<rich-text :nodes="goods_info.goods_introduce"></rich-text>
		<!-- <u-parse :html="goods_info.goods_introduce"></u-parse> -->

		<view class="bottom-nav-container">
			<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
				@buttonClick="buttonClick" />
		</view>

	</view>
</template>

<script>
	import {
		mapMutations,
		mapGetters
	} from 'vuex'

	export default {
		data() {
			return {
				goods_info: {},
				options: [{
					icon: 'shop',
					text: '店铺',
					infoBackgroundColor: '#007aff',
					infoColor: "red"
				}, {
					icon: 'cart',
					text: '购物车',
					info: 2
				}],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			}
		},
		computed:{
			...mapGetters('m_cart',['total'])
		},
		watch:{
			total:{
				handler(newVal){
					this.options.find(function(item){
						item.text === '购物车' && (item.info = newVal)
					})
				},
				immediate:true
			}
		
		},
		onLoad(options) {
			//options 可以得到页面参数
			const goods_id = options.goods_id
			this.getGoodsDetail(goods_id)
			
		},
		methods: {
			...mapMutations('m_cart', ['addToCart']),
			async getGoodsDetail(goods_id) {
				let {
					data
				} = await uni.$http.get('/api/public/v1/goods/detail', {
					goods_id
				})
				console.log(data)
				if (data.meta.status !== 200) return uni.$showMsg(data.meta.msg)
				this.goods_info = data.message
				this.goods_info.goods_introduce = data.message.goods_introduce.replace(/<img /g,
					'<img style="display:block;"')
			},
			preview(i) {
				uni.previewImage({
					current: i,
					urls: this.goods_info.pics.map(item => item.pics_big)
				})
			},
			onClick(e) {
				if (e.content.text === '购物车') {
					uni.switchTab({
						url: '../../pages/cart/cart'
					})
				}
			},
			buttonClick(e) {
				console.log(e)
				if (e.content.text === '加入购物车') {
					const goods = {
						goods_id: this.goods_info.goods_id, // 商品的Id
						goods_name: this.goods_info.goods_name, // 商品的名称
						goods_price: this.goods_info.goods_price, // 商品的价格
						goods_count: 1, //第一次添加数量默认为1 后续递增 商品的数量
						goods_small_logo: this.goods_info.goods_small_logo, // 商品的图片
						goods_state: true // 商品的勾选状态
					}
					
					this.addToCart(goods)
				}
			}
		}

	}
</script>

<style lang="scss">
	.goods-detail-container {
		padding-bottom: 50px
	}

	.bottom-nav-container {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
	}


	.goods-info-box {
		padding: 10px;

		.yf {
			font-size: 12px;
			color: gray;
			margin: 10px 0;
		}

		.price {
			font-size: 18px;
			color: #c00000;
			margin: 10px 0;

		}

		.goods-info-body {
			display: flex;
			justify-content: space-between;

			.goods-name {
				font-size: 13px;
			}

			.collect {
				display: flex;
				justify-content: center;
				flex-direction: column;
				align-items: center;
				width: 120px;
				font-size: 12px;
				color: gray;
				border-left: 1px solid #efefef;
			}
		}
	}

	swiper {
		height: 750rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}
</style>
